import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_mains_electricity_price/views/effective_data_widget.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_mains_electricity_price/views/effective_date_widgets.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_mains_electricity_price/views/item_tow_checkbox_widget.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_photovoltaic_price_set/views/photoVoltaic_stage_price_widget.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_photovoltaic_price_set/views/photovoltaic_stage_price_widgets.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/elec_price_title_widget.dart';
import 'package:yt_devops/pages/site/site_setting/site_setting_base_info/widgets/item_widget.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'add_photovoltaic_price_set_controller.dart';

class AddPhotovoltaicPriceSetPage
    extends GetView<AddPhotovoltaicPriceSetController> {
  const AddPhotovoltaicPriceSetPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AddPhotovoltaicPriceSetController>(
      builder: (controller) =>Scaffold(
        backgroundColor: Colour.backgroundColor,
      resizeToAvoidBottomInset: true,
      appBar: AppBarWidget.withRightActions(ResKey.newGridElePrice.tr),
      body:  Container(
              child: Column(children: [
                Expanded(
                    child: ListView(padding: EdgeInsets.zero, children: [
                  Container(
                    height: 10,
                    color: Colour.backgroundColor,

                  ),
                Container(
                    clipBehavior: Clip.hardEdge,
                    margin: const EdgeInsets.only(left: 16,right: 16),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(8),
                      color: Colors.white,
                    ),
                    child:Column(
                        children: [ElecPriceTitleWidget(controller.titleStringList[0]),
                  ItemWidget(
                    ResKey.siteSettingElectricityRoleName.tr,
                    controller.ruleNameController,
                    unitString: '',
                    titleColor: Colour.otherTextColor,
                    inputTextColor: Colour.mainTextColor,
                    titleFont: 14,
                    isCanEdit:true,
                    textChangeCallBack: (str){
                      controller.checkCommitStatus();
                    },
                  ),
                  ItemTowCheckboxWidget(ResKey.siteSettingElectricityEffectiveStatus.tr, ResKey.siteSettingEffective.tr,
                      ResKey.siteSettingUnEffective.tr, controller.status, (index) {})])),
                  Container(
                    height: 10,
                    color: Colour.backgroundColor,
                  ),
                      EffectiveDateWidgets(controller.timeDateList, (){
                        controller.updateTimeList();

                      }, (index){
                        controller.deleteTimeList(index);
                      },(){
                        controller.update();
                      }),
                  Container(
                    height: 10,
                    color: Colour.backgroundColor,
                  ),
                      PhotoStagePriceWidgets(controller.stagePriceList, (){
                        controller.updateStagePriceList();
                      }, (index){
                        controller.deleteStagePriceList(index);
                      }, (){
                        controller.update();
                      },(){
                        controller.update();
                      }),
                  Container(
                    height: 10,
                    color: Colour.backgroundColor,
                  ),
                ])),

              ])
      ),
        bottomNavigationBar: Container(
          color: Colour.white,
          padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
          child: YtBrnBottomButtonPanel(
              enableMainButton: controller.isEnableCommit,
              mainButtonName: ResKey.save.tr,
              mainButtonOnTap: () => controller.save(),
              secondaryButtonName: ResKey.cancel.tr,
              enableSecondaryButton: true,
              secondaryButtonBorder:
              Border.all(color: Colour.dividerColor),
              secondaryButtonOnTap: () => controller.cancelEdit()),
        ),
      ),
    );
  }
  _addEffectiveDataWidgets(BuildContext context){
    List<Widget> list = [];
    if(controller.timeDateList.length > 1){
      for(int i =1;i<controller.timeDateList.length;i++){
        var widget =  EffectiveDataWidget(context,'${ResKey.siteSettingDate.tr}${i+ 1}',
          controller.timeDateList[i]['effectiveTime'],
          controller.timeDateList[i]['expirationTime'],(startDateTime,endDateTime){
            controller.timeDateList[i] = {
              'effectiveTime':startDateTime,
              'expirationTime':endDateTime,

            };
            controller.update();
          },(){
            controller.deleteTimeList(i);
          },isDelete: true,);
        list.add(widget);
      }
    }
    return SizedBox(
      height: (controller.timeDateList.length -1) * 55,
      child: Column(
        children: list,
      ),
    );
  }
  _addStagePriceWidgets(BuildContext context) {
    List<Widget> list = [];
    if (controller.stagePriceList.length > 1) {
      for (int i = 1; i < controller.stagePriceList.length; i++) {
        var widget = PhotoVoltaicWidget(
        context,
        ResKey.siteSettingElectricityTimeStage.tr,
        controller.stagePriceList[i]['intervalStartTime'],
        controller.stagePriceList[i]['intervalEndTime'],
        controller.stagePriceList[i]['electricityFees'].toString(),
            (startDateTime, endDateTime) {
          String price =
          controller.stagePriceList[0]['electricityFees'].toString();
          controller.stagePriceList[0] = {
            'intervalStartTime': startDateTime,
            'intervalEndTime': endDateTime,
            'electricityFees': price
          };
          controller.update();
        },
            () {
              controller.deleteStagePriceList(i);
        },
            (str) {
          var map = controller.stagePriceList[i];
          map['electricityFees'] = str;
          controller.stagePriceList[i] = map;
        },isDelete: true,);
        list.add(widget);
      }
    }
    return SizedBox(
      height: (controller.stagePriceList.length - 1) * 110,
      child: Column(
        children: list,
      ),
    );
  }

}
